package com.zhike.utils.file;

import org.liuxp.minioplus.api.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.Gson;
import com.zhike.common.dto.GroupInfo4Cache;
import com.zhike.common.enums.ResultCode;
import com.zhike.common.exception.ZkimException;
import com.zhike.common.vo.UserInfo4Cache;
import com.zhike.service.store.AliStorage;
import com.zhike.service.system.ISystemConfigService;
import com.zhike.utils.cache.GroupsInfoCacheProvider;
import com.zhike.utils.cache.UsersInfoCacheProvider;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class FileAvatar {
    @Autowired
    private ISystemConfigService systemConfigService;

    @Autowired
    private UsersInfoCacheProvider usersInfoCacheProvider;

    @Autowired
    private GroupsInfoCacheProvider groupsInfoCacheProvider;

    @Autowired
    private StorageService storageService;

    /**
     * 获取用户头像网络地址
     * 
     * @param uid
     * @return
     */
    public String getUserAvatarUrl(String uid) {
        UserInfo4Cache uc = usersInfoCacheProvider.get(uid);
        if (uc == null) {
            throw new ZkimException(ResultCode.USER_NOT_EXSIT);
        }

        if (!StringUtils.isBlank(uc.getAvatar())) {
            String storage = systemConfigService.getValueByKey("storage", "default");
            if (storage.equals("minio")) {
                try {
                    String downloadUrlFull = storageService.download(uc.getAvatar(), uid);
                    return FileUtil.removeParams(downloadUrlFull);
                } catch (Exception e) {
                    log.error("用户ID: {} 获取头像异常 头像key: {}", uid, uc.getAvatar());
                    // throw new ZkimException(ResultCode.USER_AVATAR_INVALID);
                    return "";
                }
            } else if (storage.equals("aliyun")) {
                String aliyunConfigJsonStr = systemConfigService.getValueByKey("storage", "aliyun");
                if (StringUtils.isNotBlank(aliyunConfigJsonStr)) {
                    AliStorage aliStorage = new Gson().fromJson(aliyunConfigJsonStr, AliStorage.class);
                    String protocol = aliStorage.getSsl() ? "https://" : "http://";
                    return protocol + aliStorage.getDomain() + uc.getAvatar();
                }
            }
        }
        return "";
        // 返回默认头像
        // String domain = systemConfigService.getValueByKey("minio", "domain");
        // return domain + "/image/default_avatar.png";
    }

    /**
     * 获取群头像
     * 
     * @param groupId
     * @return
     */
    public String getGroupAvatarUrl(String groupId) {
        GroupInfo4Cache gc = groupsInfoCacheProvider.get(groupId);
        if (gc == null) {
            throw new ZkimException(ResultCode.GROUP_NO_EXIST);
        }
        if (!StringUtils.isBlank(gc.getAvatar())) {
            String storage = systemConfigService.getValueByKey("storage", "default");
            if (storage.equals("minio")) {
                try {
                    String downloadUrlFull = storageService.download(gc.getAvatar(), groupId);
                    return FileUtil.removeParams(downloadUrlFull);
                } catch (Exception e) {
                    log.error("群ID: {} 获取头像异常 头像key: {}", groupId, gc.getAvatar());
                    throw new ZkimException(ResultCode.USER_AVATAR_INVALID);
                }
            }
        }
        // 返回默认头像
        String domain = systemConfigService.getValueByKey("im", "file_domain");
        return domain + "/avatar/default_avatar.png";
    }

}
